package com.tky.conference.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.tky.conference.R;
import com.tky.conference.adapter.MyConferenceAdapter;
import com.tky.conference.adapter.MyConferencePubAdapter;
import com.tky.conference.base.BaseApplication;
import com.tky.conference.dao.ComeetingInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.implInterfaces.ComeetingInfoImpl;
import com.tky.conference.interfaces.HttpResultListener;
import com.tky.conference.ui.ConferenceRecordActivity;
import com.tky.conference.ui.FZDetailActivity;
import com.tky.conference.ui.MainActivity;
import com.tky.conference.ui.MyConferenceUpdateActivity;
import com.tky.conference.ui.ZuZhiDetailActivity;
import com.tky.conference.utlils.ShowDialog;
import com.tky.conference.utlils.ToastUtil;
import com.tky.conference.utlils.UIUtils;

import java.util.List;

/**
 * Created by cll on 2015/12/14.
 */
public class DutyFragment extends Fragment implements HttpResultListener,AdapterView.OnItemClickListener{
    private ListView appointListView;
    private List<ComeetingInfo> comeetingInfoList;
    private MyConferencePubAdapter myConferencePubAdapter;
    private ComeetingInfoImpl comeetingInfoImpl;
    private ShowDialog showDialog;
    private String account;
    public static DutyFragment newInstance(){
        DutyFragment f = new DutyFragment();
        return f;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_zhuzhi,null);
        appointListView= (ListView) view.findViewById(R.id.zhuzhi_fragment);
        SharedPreferences sharedPreferences=this.getActivity().getSharedPreferences("save_account", Context.MODE_PRIVATE);
        account=sharedPreferences.getString("account",null);
//        conferenceAdapter=new MyConferenceAdapter(BaseApplication.getContext(),comeetingInfoList);
//        appointListView.setAdapter(conferenceAdapter);
//        appointListView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            //fragment可见时加载数据
            comeetingInfoImpl=new ComeetingInfoImpl(this);
            comeetingInfoImpl.getComeetingInfo(account,"2","1");
        } else {
            //不可见时不执行操作
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onStartRequest() {
        if (showDialog == null) {
            showDialog = new ShowDialog(this.getActivity());
        }
        showDialog.Loading("正在加载数据...");
    }

    @Override
    public void onResult(Object obj) {
        showDialog.dismissDialog();
        FailRequest failRequestobj=new FailRequest();
        if(obj instanceof FailRequest){
            if(!failRequestobj.getStatus().equals(0)){
                Toast.makeText(this.getActivity(), failRequestobj.getMsg(), Toast.LENGTH_SHORT).show();
            }
        }else{
            comeetingInfoList=(List<ComeetingInfo>)obj;
            if(comeetingInfoList.size()>0){
                myConferencePubAdapter=new MyConferencePubAdapter(this.getActivity(),comeetingInfoList);
                appointListView.setAdapter(myConferencePubAdapter);
                appointListView.setOnItemClickListener(this);
            }else{
                ToastUtil.showToast(this.getActivity(),"该会议列表没有数据");
            }

        }
    }

    @Override
    public void onFinish() {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        /**获取单个item数据对象*/
        ComeetingInfo comeetingInfo=new ComeetingInfo();
        comeetingInfo=comeetingInfoList.get(i);
        Bundle bundle = new Bundle();
        bundle.putSerializable("duty", comeetingInfo);
        //当条目被点击时候获取详细信息
        Intent intent=new Intent(BaseApplication.getContext(),FZDetailActivity.class);
        intent.putExtras(bundle);
        UIUtils.startActivity(intent);
    }
}
